﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Globalization;
using System.Threading;
using System.Resources;
using DataAccess.BusinessObject;
using DataAccess.DataObject;
using System.Collections.Generic;

namespace EnmineKaitlin
{
    public partial class faq : Common.PageBase
    {        
        public int CatID = 0;
        public int PostID = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Request.Params["cat"] != null)
                    {
                        string strcat = Request.Params["cat"].ToString();
                        strcat = strcat.Substring(strcat.LastIndexOf("-") + 1);
                        CatID = int.Parse(strcat);
                    }
                    if (CatID > 0)
                    {
                        if (Request.Params["p"] != null && int.Parse(Request.Params["p"].ToString()) > 0)
                        {
                            string strpost = Request.Params["p"].ToString();
                            strpost = strpost.Substring(strpost.LastIndexOf("-") + 1);
                            PostID = int.Parse(strpost);
                        }
                        BindPage(CatID, PostID);
                    }
                    rptBannerSubPage.DataSource = _BPost.Get(Common.PageBase.BannerTrai_PostID);
                    rptBannerSubPage.DataBind();
                    LoadString();
                }
                catch (Exception ex)
                {
                    Response.Redirect("~/home");
                }      
          
                                                
            }                       
        }
        protected string GetString(string strid)
        {
            return rm.GetString(strid,ci) ;
        }
        protected void LoadString()
        {
            lblAsk.Text = GetString("ask");
            lblCategory.Text = GetString("category");
            lblName.Text = GetString("name");
            lblQuestion.Text = GetString("question");
            btnSend.Text = GetString("send_question");
            lbtCancel.Text = GetString("cancel");
        }
        protected void BindPage(int CatID, int PostID)
        {
            pnlnavleft.Visible = true;
            IList<OCategory> lstCategory = _BCategory.Get(CatID, "");
            IList<OCategory> lstCatParent;
            int CatParent = lstCategory[0].CatParent;
            IList<OCategory> lstCatChild = _BCategory.Get(0, CatID, "");            
            //Load ảnh đại diện của chuyên mục
            if (CatParent > 0)
            {
                lstCatParent = _BCategory.Get(lstCategory[0].CatParent, "");
                lblCatName.Text = lstCatParent[0].CatName;
                imgBanner.ImageUrl = lstCatParent[0].FeaturedImage;
                imgBanner.DataBind();
                //Bind sidebar leFt
                rptListCat.DataSource = _BCategory.Get(0, lstCatParent[0].CatID, "");
                rptListCat.DataBind();
                //Bind content
                lblTitle.Text = get_category_name(CatID);
                rptFAQ.DataSource = _BFAQ.Get(0, "publish", CatID, "SendDate", "DESC", 1, 50);
                rptFAQ.DataBind();

                ddlCategory.DataSource = _BCategory.Get(0, CatParent, "");
                ddlCategory.DataBind();
            }
            else
            {
                lblCatName.Text = lstCategory[0].CatName;
                imgBanner.ImageUrl = lstCategory[0].FeaturedImage;
                imgBanner.DataBind();
                if (lstCatChild.Count > 0)
                {
                    rptListCat.DataSource = lstCatChild;
                    rptListCat.DataBind();
                    //Bind content
                    lblTitle.Text = get_category_name(lstCatChild[0].CatID);
                    rptFAQ.DataSource = _BFAQ.Get(0, "publish", lstCatChild[0].CatID, "SendDate", "DESC", 1, 50);
                    rptFAQ.DataBind();

                    ddlCategory.DataSource = lstCatChild;
                    ddlCategory.DataBind();
                }
                else
                {
                    rptListLeft.DataSource = _BFAQ.Get(0,"publish",CatID,"SendDate","DESC",1, 50);
                    rptListLeft.DataBind();
                }
            }
        }
        protected void btnSend_Click(object sender, EventArgs e)
        {
            _OFAQ.Question = txtQuestion.Text;
            _OFAQ.Sender = txtName.Text;
            _OFAQ.SendDate = DateTime.Now;
            _OFAQ.CatID = int.Parse(ddlCategory.SelectedValue);
            _OFAQ.Status = "pending";
            _OFAQ.Answer = "";
            _OFAQ.Replyer = "";
            _OFAQ.ReplyDate = DateTime.Now;            
            if (_BFAQ.Add(_OFAQ))
            {
                RegisterClientScriptBlock("Note", "<script>alert('Cảm ơn bạn đã gửi câu hỏi, ban quản trị EnmineKaitlin sẽ sớm trả lời câu hỏi của bạn.');</script>");
                txtName.Text = "";
                txtQuestion.Text = "";
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            txtName.Text = "";
            txtQuestion.Text = "";
        }
    }
}
